jQuery.extend({
    createDownloadIframe: function(id, uri){
			//创建iframe
            var frameId = 'jDownloadFrame' + id;
            var iframeHtml = '<iframe id="' + frameId + '" name="' + frameId + '" style="position:absolute; top:-9999px; left:-9999px"';
			if(window.ActiveXObject){
                if(typeof uri === 'boolean'){
					iframeHtml += ' src="' + 'javascript:false' + '"';
                }
                else if(typeof uri === 'string'){
					iframeHtml += ' src="' + uri + '"';
                }	
			}
			iframeHtml += ' />';
			jQuery(iframeHtml).appendTo(document.body);

            return jQuery('#' + frameId).get(0);			
    },
    createDownloadForm: function(id, data){
		//创建表单域
		var formId = 'jDownloadForm' + id;
		var form = jQuery('<form  action="" method="POST" name="' + formId + '" id="' + formId + '"></form>');	
		if(data){
			for(var i in data){
				jQuery('<input type="hidden" name="' + i + '" value="' + data[i] + '" />').appendTo(form);
			}			
		}		
		
		jQuery(form).css('position', 'absolute');
		jQuery(form).css('top', '-1200px');
		jQuery(form).css('left', '-1200px');
		jQuery(form).appendTo('body');		
		return form;
    },

    ajaxExcelDownload: function(s){
        s = jQuery.extend({}, jQuery.ajaxSettings, s);
        var id = new Date().getTime()        
		var form = jQuery.createDownloadForm(id, (typeof(s.data) === 'undefined'? false : s.data));
		var iframe = jQuery.createDownloadIframe(id, s.secureuri);
		var frameId = 'jDownloadFrame' + id;
        if (s.global && ! jQuery.active++){
			jQuery.event.trigger( "ajaxStart" );
		}            
        var requestDone = false;
        var xml = {}   
        if (s.global){
        	jQuery.event.trigger("ajaxSend", [xml, s]);
        }
            
        var downloadCallback = function(isTimeout){			
			var io = document.getElementById(frameId);
            try{				
				if(io.contentWindow){
					xml.responseText = io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;
					xml.responseXML = io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
				}
				else if(io.contentDocument){
					xml.responseText = io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null;
                	xml.responseXML = io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document;
				}						
            }catch(e){
				jQuery.handleError(s, xml, null, e);
			}
            
            if (xml || isTimeout === "timeout") {				
                requestDone = true;
                var status;
                try {
                    status = isTimeout !== "timeout" ? "success" : "error";
                    if ( status !== "error"){
                        var data = jQuery.downloadHttpData( xml, s.dataType );    
                        if ( s.success ){
                        	s.success( data, status );
                        }
                            
                        if( s.global ){
                        	jQuery.event.trigger( "ajaxSuccess", [xml, s] );
                        }
                    }else{
                    	jQuery.handleError(s, xml, status);
                    }
                }catch(e) {
                    status = "error";
                   	jQuery.handleError(s, xml, status, e);
                }

                if( s.global ){
                	jQuery.event.trigger( "ajaxComplete", [xml, s] );
                }
                    

                if ( s.global && ! --jQuery.active ){
                	jQuery.event.trigger( "ajaxStop" );
                }
                    

                if ( s.complete ){
                	s.complete(xml, status);
                }

                jQuery(iframe).unbind()

                setTimeout(function(){	
                	try {
						jQuery(iframe).remove();
						jQuery(form).remove();	
						
					}catch(e){
						jQuery.handleError(s, xml, null, e);
					}									

				}, 100)

                xml = null
            }
        }
        
        if ( s.timeout > 0 ) {
            setTimeout(function(){
                if(!requestDone){
                	downloadCallback( "timeout" );
                } 
            }, s.timeout);
        }
        
        try{
			jQuery(form).attr('action', s.url);
			jQuery(form).attr('method', 'POST');
			jQuery(form).attr('target', frameId);
            			
            jQuery(form).submit();

        }catch(e){			
            jQuery.handleError(s, xml, null, e);
        }
		
		jQuery('#' + frameId).load(downloadCallback);
        return {abort: function () {}};	
    },

    downloadHttpData: function(r, type) {
        var data = !type;
        data = type === "xml" || data ? r.responseXML : r.responseText;
        if ( type === "script" ){
        	jQuery.globalEval( data );
        }
            
        if ( type === "json" ){
        	//返回json字符串中含有pre标签信息，需替换掉
        	data = data.replace(/[<][\/]?(pre)[\s]*(style=\"word-wrap: break-word; white-space: pre-wrap;\")*[>]/gi,"");
            eval( "data = " + data);
        }
        	
        if ( type === "html" ){
        	jQuery("<div>").html(data).evalScripts();
        }

        return data;
    },
    
    handleError: function( s, xhr, status, e ) {
        // If a local callback was specified, fire it
		if ( s.error ) {
			s.error.call( s.context || s, xhr, status, e);
		}
		
		// Fire the global callback
		if ( s.global ) {
			(s.context ? jQuery(s.context) : jQuery.event).trigger( "ajaxError", [xhr, s, e]);
		}
    }
})